package com.newway.back.controller.approval;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newway.back.dto.approval.CreateApplyDto;
import com.newway.back.dto.approval.response.ApplyListDto;
import com.newway.back.entity.SysUserEntity;
import com.newway.back.entity.approval.ApprovalInstance;
import com.newway.back.entity.approval.ApprovalNode;
import com.newway.back.entity.approval.ApprovalTask;
import com.newway.back.entity.approval.ApprovalTemplate;
import com.newway.back.enums.ApprovalStatusEnum;
import com.newway.back.enums.ApproverTypeEnum;
import com.newway.back.enums.TaskStatusEnum;
import com.newway.back.mapper.SysUserMapper;
import com.newway.back.mapper.approval.ApprovalInstanceMapper;
import com.newway.back.mapper.approval.ApprovalNodeMapper;
import com.newway.back.mapper.approval.ApprovalTaskMapper;
import com.newway.back.mapper.approval.ApprovalTemplateMapper;
import com.newway.back.service.impl.ApprovalCommonService;
import com.newway.back.util.SecurityUserContext;
import com.newway.common.MyPages;
import com.newway.common.Result;
import com.newway.exception.BusinessException;
import com.newway.wechat.entity.sign.SignDataEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("/back/approval/apply")
@RestController
public class ApprovalApplyController {


    @Resource
    ApprovalTemplateMapper approvalTemplateMapper;

    @Resource
    ApprovalNodeMapper approvalNodeMapper;

    @Resource
    ApprovalInstanceMapper approvalInstanceMapper;

    @Resource
    ApprovalTaskMapper approvalTaskMapper;

    @Resource
    SysUserMapper sysUserMapper;

    @Resource
    ApprovalCommonService approvalCommonService;

    /**
     * 查询我的申请
     * @param approvalTask
     * @return
     */
    @GetMapping("/list")
    public Result getList(ApprovalTask approvalTask){
        List<ApplyListDto> list = approvalInstanceMapper.getMyApplyList(SecurityUserContext.getCurrentUserInfo().getId());
        MyPages<ApplyListDto> selectPage = new MyPages<>();
        selectPage.setTotal(list.size());
        selectPage.setRecords(list);
        selectPage.setPages(1);
        return Result.success(selectPage);
    }

    /**
     * 发起申请
     * @param createApplyDto
     * @return
     */
    @PostMapping("/createApply")
    @Transactional
    public Result applyApproval(@RequestBody CreateApplyDto createApplyDto){
        ApprovalTemplate approvalTemplate = approvalTemplateMapper.selectById(createApplyDto.getTemplateId());
        // 1. 创建审批实例
        ApprovalInstance instance = new ApprovalInstance();
        instance.setTemplateId(createApplyDto.getTemplateId());
        instance.setTemplateVersion(approvalTemplate.getVersion());
        instance.setTitle(approvalTemplate.getTemplateName()+"申请");
        instance.setApplicantId(SecurityUserContext.getCurrentUserInfo().getId());
        instance.setFormData(createApplyDto.getApplyData());
        instance.setCurrentStatus(ApprovalStatusEnum.PENDING.getValue());
        approvalInstanceMapper.insert(instance);
        // 2. 获取第一个审批节点
        ApprovalNode firstNode = approvalCommonService.getFirstNode(createApplyDto.getTemplateId());
        // 3. 确定审批人
        Integer approverId = approvalCommonService.determineApprover(firstNode);

        // 4. 创建审批任务
        ApprovalTask task = new ApprovalTask();
        task.setInstanceId(instance.getInstanceId());
        task.setNodeId(firstNode.getNodeId());
        task.setApproverId(approverId);
        task.setStatus(TaskStatusEnum.PENDING.getValue());
        approvalTaskMapper.insert(task);
        // 5. 更新实例当前节点
        instance.setCurrentNodeId(firstNode.getNodeId());
        approvalInstanceMapper.updateById(instance);
        return Result.success();
    }


}
